package cn.iocoder.yudao.module.enterprise.service.organization;

import cn.iocoder.yudao.module.enterprise.controller.admin.organization.vo.OrganizationCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.organization.vo.OrganizationUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.organization.OrganizationConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.controlplan.ControlPlanDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.controlplan.ControlPlanMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.organization.OrganizationMapper;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import cn.iocoder.yudao.module.system.api.tenant.dto.TenantRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.ORGANIZATION_NOT_EXISTS;

/**
 * 组织基本信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrganizationServiceImpl implements OrganizationService {

    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private ControlPlanMapper controlPlanMapper;
    @Resource
    private TenantApi tenantApi;

    @Override
    public Long createOrganization(OrganizationCreateReqVO createReqVO) {
        // 插入
        OrganizationDO organization = OrganizationConvert.INSTANCE.convert(createReqVO);
        // 查询计划信息
        ControlPlanDO controlPlan = controlPlanMapper.selectById(createReqVO.getPlanId());
        // 查询组织信息
        TenantRespDTO tenant = tenantApi.getTenant(controlPlan.getTenantId()).getCheckedData();
        organization.setName(tenant.getName())
                .setIndustry(tenant.getIndustry());

        organizationMapper.insert(organization);
        // 返回
        return organization.getId();
    }

    @Override
    public void updateOrganization(OrganizationUpdateReqVO updateReqVO) {
        // 校验存在
        validateOrganizationExists(updateReqVO.getId());
        // 更新
        OrganizationDO updateObj = OrganizationConvert.INSTANCE.convert(updateReqVO);
        organizationMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrganization(Long id) {
        // 校验存在
        validateOrganizationExists(id);
        // 删除
        organizationMapper.deleteById(id);
    }

    private void validateOrganizationExists(Long id) {
        if (organizationMapper.selectById(id) == null) {
            throw exception(ORGANIZATION_NOT_EXISTS);
        }
    }

    @Override
    public OrganizationDO getOrganization(Long id) {
        return organizationMapper.selectById(id);
    }

    @Override
    public OrganizationDO getOrganizationByPlanId(Long planId) {
        return organizationMapper.selectByPlanId(planId);
    }
}
